package cc.vv.btpartyjob;

import android.content.Context;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.util.Log;


import com.tencent.smtt.sdk.QbSdk;

import java.io.IOException;

import cc.vv.BaseNewApplication;
import cc.vv.baselibrary.db.BaseDBHelper;
import cc.vv.baselibrary.global.BCSharePreKey;
import cc.vv.baselibrary.global.UserInfoSharePreKey;
import cc.vv.baselibrary.util.LK;
import cc.vv.baselibrary.util.LKPrefUtil;
import cc.vv.baselibrary.util.TokenUtil;
import cc.vv.baselibrary.util.UserInfoManageUtil;
import cc.vv.baselibrary.util.router.RouterConfig;
import cc.vv.lkbasecomponent.http.LKHttp;
import cc.vv.lkbasecomponent.http.lib.wrap.InterceptorWrap;
import cc.vv.lkimagecomponent2.LKImage;
import cc.vv.lkimagecomponent2.lib.mode.DiskCacheMode;
import cc.vv.lklibrary.log.LogOperate;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 项目Application
 *
 * @author: sun.li
 * @date: 2018/9/5.
 */
public class AppApplication extends BaseNewApplication {

    @Override
    public void onCreate() {
        super.onCreate();
        RouterConfig.getInstance().initRouter();
        LKPrefUtil.createPref(this, BCSharePreKey.FILENAME_SP);
        LK.Ext.init(this);
        LK.Ext.setDebug(true);

        //初始化图片加载框架
        LKImage.init(getApplicationContext(), "LKIMAGE_TAG").gifRepeatCount(-1)
                .cacheMaxSize(400).cacheFileName("imageCache").diskCacheStrategy(DiskCacheMode.AUTOMATIC)
                .over();

        //初始化请求类型为RAW/JSON.框架默认为Form
//        LKHttp.init().initRequestType(MediaTypeWrap.MEDIA_TYPE_RAW_JSON);
//        setHttpHead();

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());

        //初始化数据库
        BaseDBHelper.initInstance(this);
        initTBS();
        String token = UserInfoManageUtil.getToken();
        Log.e("lk_chen", "resulttoken===================数据===============" + token);
        if (!TextUtils.isEmpty(token)) {
            setHttpHead();
        }
    }


    /**
     * 设置token的请求头
     */
    private void setHttpHead() {
        //okhttp添加请求头
        LKHttp.init().addIntercepter(new InterceptorWrap() {
            @Override
            protected Response interceptWrap(Chain chain) throws IOException {
                Request request = chain.request();
                try {
                    //默认传的token是accessToken.如果刷新token请求,header中token就为refresh_token
                    String token = UserInfoManageUtil.getToken();
                    request = request.newBuilder()
                            .addHeader("X-Auth-Token", token).addHeader("platform", "app")
                            .build();
                } catch (NullPointerException e) {
                    LogOperate.e("请求拦截器空指针", e);
                }
                return chain.proceed(request);

            }
        }, true);
    }


    /**
     * 初始化TBS浏览服务X5内核
     */
    private void initTBS() {
        //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        //非wifi条件下允许下载X5内核
        QbSdk.setDownloadWithoutWifi(true);
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                Log.e("lk_chen", "result==" + "腾讯tbs初始化---" + arg0);
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
            }

            @Override
            public void onCoreInitFinished() {
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), cb);
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}